package com.yfjin.wifiadbd;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;

/**
 * @author jinyf
 */
public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void execShell(String cmd) {
        try {
            //权限设置
            Process p = Runtime.getRuntime().exec("su");
            //获取输出流
            OutputStream outputStream = p.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            //将命令写入
            dataOutputStream.writeBytes(cmd);
            //提交命令
            dataOutputStream.flush();
            //关闭流操作
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
            t.printStackTrace();
        }
    }

    public void serprop_555(View view) {
        execShell("setprop service.adb.tcp.port 5555");
    }

    public void start_adbd(View view) {
        execShell("stop adbd");
    }

    public void stop_adbd(View view) {
        execShell("start adbd");
    }

    public void all(View view) {
//        serprop_555(view);
//        stop_adbd(view);
//        start_adbd(view);
        Toast.makeText(this, getSerialNo(), Toast.LENGTH_SHORT).show();
    }




    public static String getSerialNo() {
        String[] propertys = {"ro.root.serialno", "ro.boot.serialno", "ro.serialno", "gsm-serialno"};
        for (String key : propertys) {
            String value = getAndroidOsSystemProperties(key);
            if (!TextUtils.isEmpty(value)) {
                Log.e("TAG", "serial==>" + key + " : " + value);
                return value;
            }
        }
        return "";
    }

    private static String getAndroidOsSystemProperties(String key) {
        String value = "";
        try {
            Method systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
            if ((value = (String) systemProperties_get.invoke(null, key)) != null)
                return value;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }
}